window.addEventListener('load', function () {
    // 公共部分
    let ul = document.querySelector('header>ul')
    let lis = document.querySelectorAll('header>ul>li')
    let sections = document.querySelectorAll('main>section')
    ul.addEventListener('click', function (e) {
        clearStyle()
        e.target.classList = 'current'
        sections[e.target.dataset.set].classList = 'current'
    })
    function clearStyle() {
        lis.forEach(item => item.classList = '')
        sections.forEach(item => item.classList = '')
    }

    // 新闻部分
    let ulList = document.querySelector('#news ul')
    axios.get('https://mobile-ms.uat.homecreditcfc.cn/mock/61727df60155e30027287be1/example/topnews')
        .then(res => {
            let Inner = template('tpl-news', res.data.result)
            ulList.innerHTML = Inner
        })
        .catch(err => {
            // console.error(err); 
        })

    // 笑话部分
    let liXhr = new XMLHttpRequest()
    liXhr.open('get', 'https://mobile-ms.uat.homecreditcfc.cn/mock/61727df60155e30027287be1/example/joke')
    liXhr.send()
    liXhr.onreadystatechange = function () {
        if (liXhr.readyState === 4 && liXhr.status === 200) {
            let str = liXhr.responseText
            renderJoke(JSON.parse(str))
        }
    }
    function renderJoke(data) {
        joke.innerHTML = template('liJoke', data.result)
    }

    // 历史部分
    historyLi.addEventListener("click", function () {
        let historyContent = document.querySelectorAll('.hisorytitle')
        if (historyContent.length != 0) {
            return;
        }
        getHistory();

    })
    function getHistory() {
        axios({
            method: 'GET',
            url: "https://mobile-ms.uat.homecreditcfc.cn/mock/61727df60155e30027287be1/example/todayOnhistory"
        }).then(res => {
            if (res.status == 200) {
                renderHistory(res.data)
            }
        })
    }
    function renderHistory(data) {
        let historyStr = template("tpl_history", data)
        let History = document.getElementById("history")
        History.innerHTML = '';
        History.innerHTML = historyStr;
    }

    // NBA部分
    axios({
        method: 'GET',
        url: "https://mobile-ms.uat.homecreditcfc.cn/mock/61727df60155e30027287be1/example/nba",
    }).then(function (res) {
        let act = res.data.result
        let arr = template("nbatext", act) //出现问题！前面id 是 nba结果发现并不渲染，是因为id冲突了
        let nba = document.querySelector("#nba")
        //渲染字符串
        nba.innerHTML = arr
    })
})